package com.fliangyun.nsa.nsamanage.service.impl;

import com.fliangyun.nsa.nsamanage.dao.LogInfoMapper;
import com.fliangyun.nsa.nsamanage.entities.LogInfo;
import com.fliangyun.nsa.nsamanage.entities.User;
import com.fliangyun.nsa.nsamanage.service.LogInfoService;
import com.fliangyun.nsa.nsamanage.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author fuliangyun@139.com
 * @date 2023年05月30日
 * @description
 */
@Service
public class LogInfoServiceImpl implements LogInfoService {
    @Autowired
    private LogInfoMapper logInfoMapper;

    @Override
    public void save(LogInfo logInfo) {
        logInfo.setId(StringUtil.uuid());
        logInfo.setIp(StringUtil.getIpAddress());
        logInfo.setCreateTime(new Date());
        Authentication authentication =SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null){
            User user = (User) authentication.getPrincipal();
            logInfo.setUserId(user.getMobile());
        }
        logInfoMapper.insert(logInfo);
    }
}
